package com.example.lib_image.glide.glide

import android.content.Context
import android.graphics.Bitmap
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.view.View
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.MultiTransformation
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.example.lib_base.R
import jp.wasabeef.glide.transformations.BlurTransformation
import jp.wasabeef.glide.transformations.RoundedCornersTransformation

/**
 * Glide图片加载工具
 */
object GlideImgLoader {

    /**
     * 加载普通图片
     */
    fun loadImage(context: Context, url: String, imageView: ImageView) {
        Glide.with(context)
            .load(url)
            .placeholder(ColorDrawable(Color.GRAY))
            .error(ColorDrawable(Color.YELLOW))
            .thumbnail(0.5F) //设置缩略图
            .skipMemoryCache(false) //跳过内存缓存
            .diskCacheStrategy(DiskCacheStrategy.RESOURCE) //磁盘缓存策略：NONE, ALL，DATA，RESOURCE，AUTOMATIC
            .transform(CenterCrop())
            .into(imageView)
    }

    /**
     * 加载圆形图片
     */
    fun loadCircleImage(context: Context, url: String, imageView: ImageView) {
        Glide.with(context)
            .load(url)
            .placeholder(ColorDrawable(Color.GRAY))
            .error(ColorDrawable(Color.YELLOW))
            .skipMemoryCache(true)
            .diskCacheStrategy(DiskCacheStrategy.RESOURCE) //磁盘缓存策略：NONE, ALL，DATA，RESOURCE，AUTOMATIC
            .transform(CenterCrop(), CircleCrop())
            .into(imageView)
    }

    /**
     * 加载圆角图片
     */
    fun loadBoundImage(context: Context, url: String, radio: Int, imageView: ImageView) {
        Glide.with(context)
            .load(url)
            .placeholder(ColorDrawable(Color.GRAY))
            .error(ColorDrawable(Color.YELLOW))
            .skipMemoryCache(true)
            .transform(CenterCrop(), RoundedCorners(radio))
            .into(imageView)
    }

    /**
     * 加载本地资源图片
     */
    fun loadResImage(view: View, resId: Int, imageView: ImageView) {
        Glide.with(view)
            .load(resId)
            .skipMemoryCache(true)
            .transform(CenterCrop())
            .into(imageView)
    }

    fun loadBlurImage(context: Context,url: String,imageView: ImageView){
        Glide.with(context)
            .load(url)
            .placeholder(ColorDrawable(Color.GRAY))
            .error(ColorDrawable(Color.YELLOW))
            .thumbnail(0.5F) //设置缩略图
            .skipMemoryCache(false) //跳过内存缓存
            .diskCacheStrategy(DiskCacheStrategy.RESOURCE) //磁盘缓存策略：NONE, ALL，DATA，RESOURCE，AUTOMATIC
            .apply(RequestOptions.bitmapTransform(MultiTransformation<Bitmap>(
                BlurTransformation(10,3),
                RoundedCornersTransformation(128,0,RoundedCornersTransformation.CornerType.BOTTOM)
                )
            ))
            .into(imageView)
    }

}